package com.kalix.jyksh.proint.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.jyksh.proint.entities.*;
import com.kalix.ksh.proint.api.biz.IRealtimeBeanService;
import com.kalix.ksh.proint.api.dao.IRealtimeBeanDao;
import com.kalix.ksh.proint.api.model.InfUtil;
import com.kalix.ksh.proint.entities.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Wanghw on 2018/10/10.
 * 设备运行状态请求
 */
public class RealtimeBeanServiceImpl extends GenericBizServiceImpl<IRealtimeBeanDao, RealtimeBean> implements IRealtimeBeanService {
    public RealtimeBeanServiceImpl(){
        super.init(RealtimeBean.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");

   /**
    *
    * 功能描述:根据设备运行状态需要数据格式返回数据
    *
    * @auther: Whw
    * @date: 2019/1/14 0014 17:17
    * @param:
    * @return:
    */

    public String getRealValue() {
        String sql = "SELECT s FROM SummaryBean s";
        List<SummaryBean> summary = dao.find(sql);
        JSONArray host = new JSONArray();
        JSONArray database = new JSONArray();
        JSONArray service = new JSONArray();
        JSONArray network = new JSONArray();
        for (int i = 0; i < summary.size() ; i++) {
            JSONObject hostnum =  gethost(summary.get(i).getName());
            host.put(hostnum);
            JSONObject databasenum =  getdatabase(summary.get(i).getName());
            database.put(databasenum);
            JSONObject servicenum =  getservice(summary.get(i).getName());
            service.put(servicenum);
            JSONObject networknum =  getnetwork(summary.get(i).getName());
            network.put(networknum);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Host", host);
        jsonObject.put("network", network);
        jsonObject.put("app", service);
        jsonObject.put("database", database);

//            System.out.println(summary.get(i).getName());

        return jsonObject.toString();
    }

    /**
     * 根据应用名称查询主机UUID并返回值
     * @param value
     * @return
     */
    public JSONObject gethost(String value){
        String sql = "SELECT s FROM HistogramTitleBean s WHERE s.appname = '"+value +"'";
        List<HistogramTitleBean> list = dao.find(sql);
        int ON = 0;
        int OFF = 0;

        JSONObject jsonOb = new JSONObject();
        JSONArray ja = new JSONArray();
        if(list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getUuid() == null ){
                    OFF += 1;
                }else {
                    //根据操作系统判断发送请求
                    if(list.get(i).getSystem().equals("Windows")){
                        String uri = "http://192.168.31.252/api/v1/mo/Windows/00100090/metrics/PingStatus";
//                    String uri ="http://"+wsIp+"/api/v1/mo/Windows/"+list.get(i).getUuid()+"/metrics/PingStatus";
                        String data= InfUtil.interfaceUtil(uri,"");
                        JSONObject jsonObject = new JSONObject(data);
                        // 判断是否是好使状态
                        String values = jsonObject.get("value").toString();
                        if(values.equals("ON")){
                            ON += 1;
                        }else {
                            ja.put(list.get(i).getUuid());
                        }

                    }else {
                        String uri = "http://192.168.31.252/api/v1/mo/Linux/00100090/metrics/PingStatus";
//                    String uri ="http://"+wsIp+"/api/v1/mo/Linux/"+list.get(i).getUuid()+"/metrics/PingStatus";
                        String data= InfUtil.interfaceUtil(uri,"");
                        JSONObject jsonObject = new JSONObject(data);
                        // 判断是否是好使状态
                        String values = jsonObject.get("value").toString();
                        if(values.equals("ON")){
                            ON += 1;
                        }else {
                            ja.put(list.get(i).getUuid());
                        }
                    }
                }
            }
            if (ja.length()>0) {
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",3);
                jsonOb.put("msg",ja);
                return jsonOb;
            }else if(ON > 0){
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",1);
                return jsonOb;
            }else {
                jsonOb.put("name",value);
                jsonOb.put("value",OFF);
                jsonOb.put("type",2);
                return jsonOb;
            }

        }else {
            jsonOb.put("name",value);
            jsonOb.put("value",0);
            jsonOb.put("type",0);
            return jsonOb;
        }


//        return jsonOb;
    }

    /**
     * 根据应用名称查询数据库UUID并返回值
     * @param value
     * @return
     */
    public JSONObject getdatabase(String value){
        String sql = "SELECT s FROM HistogramRequestBean s WHERE s.appname = '"+value +"'";
        List<HistogramRequestBean> list = dao.find(sql);
        int ON = 0;
        int OFF = 0;

        JSONObject jsonOb = new JSONObject();
        JSONArray ja = new JSONArray();
        if(list.size() > 0){
        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i).getUuid());
            if(list.get(i).getUuid() == null ){
                OFF += 1;
            }else {
                //根据操作系统判断发送请求
                if(list.get(i).getDatabase().equals("mysql")){
                    String uri = "http://192.168.31.252/api/v1/mo/MySQL/01000075/metrics/DBPing";
//                    String uri ="http://"+wsIp+"/api/v1/mo/MySQL/"+list.get(i).getUuid()+"/metrics/DBPing";
                    String data= InfUtil.interfaceUtil(uri,"");
                    JSONObject jsonObject = new JSONObject(data);
                    // 判断是否是好使状态
                    String values = jsonObject.get("value").toString();
                    if(values.equals("1")){
                        ON += 1;
                    }else {
                        ja.put(list.get(i).getUuid());
                    }
                }else if (list.get(i).getDatabase().equals("Oracle")){
                    String uri = "http://192.168.31.252/api/v1/mo/Oracle/01000075/metrics/DBPing";
//                    String uri ="http://"+wsIp+"/api/v1/mo/Oracle/"+list.get(i).getUuid()+"/metrics/DBPing";
                    String data= InfUtil.interfaceUtil(uri,"");
                    JSONObject jsonObject = new JSONObject(data);
                    // 判断是否是好使状态
                    String values = jsonObject.get("value").toString();
                    if(values.equals("1")){
                        ON += 1;
                    }else {
                        ja.put(list.get(i).getUuid());
                    }
                }else {
                    String uri = "http://192.168.31.252/api/v1/mo/Sql/01000075/metrics/DBPing";
//                    String uri ="http://"+wsIp+"/api/v1/mo/Sql/"+list.get(i).getUuid()+"/metrics/DBPing";
                    String data= InfUtil.interfaceUtil(uri,"");
                    JSONObject jsonObject = new JSONObject(data);
                    // 判断是否是好使状态
                    String values = jsonObject.get("value").toString();
                    if(values.equals("1")){
                        ON += 1;
                    }else {
                        ja.put(list.get(i).getUuid());
                    }
                }

            }
        }
            if (ja.length()>0) {
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",3);
                jsonOb.put("msg",ja);
                return jsonOb;
            }else if(ON > 0){
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",1);
                return jsonOb;
            }else {
                jsonOb.put("name",value);
                jsonOb.put("value",OFF);
                jsonOb.put("type",2);
                return jsonOb;
            }

        }else {
            jsonOb.put("name",value);
            jsonOb.put("value",0);
            jsonOb.put("type",0);
            return jsonOb;
        }

    }

    /**
     * 根据标准应用查询数据并返回值
     * @param value
     * @return
     */
    public JSONObject getservice(String value){
        String sql = "SELECT s FROM RunSitBean s WHERE s.appname = '"+value +"'";
        List<RunSitBean> list = dao.find(sql);
        int ON = 0;
        int OFF = 0;

        JSONObject jsonOb = new JSONObject();
        JSONArray ja = new JSONArray();
        if(list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
//                System.out.println(list.get(i).getUuid());
                if (list.get(i).getUuid() == null) {
                    OFF += 1;
                } else {
                    //根据uuid系统判断发送请求
                    String uri = "http://192.168.31.252/api/v1/mo/HTTP/0300007f/metrics/ServicePing";
//                String uri ="http://"+wsIp+"/api/v1/mo/HTTP/"+list.get(i).getUuid()+"/metrics/PingStatus";
                    String data = InfUtil.interfaceUtil(uri, "");
                    JSONObject jsonObject = new JSONObject(data);
                    // 判断是否是好使状态
                    String values = jsonObject.get("value").toString();
                    if (values.equals("Successed")) {
                        ON += 1;
                    } else {
                        ja.put(list.get(i).getUuid());
                    }

                }
            }
            if (ja.length()>0) {
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",3);
                jsonOb.put("msg",ja);
                return jsonOb;
            }else if(ON > 0){
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",1);
                return jsonOb;
            }else {
                jsonOb.put("name",value);
                jsonOb.put("value",OFF);
                jsonOb.put("type",2);
                return jsonOb;
            }
        }else {
            jsonOb.put("name",value);
            jsonOb.put("value",0);
            jsonOb.put("type",0);
            return jsonOb;
        }

    }


    /**
     * 根据网络设备查询数据并返回值
     * @param value
     * @return
     */
    public JSONObject getnetwork(String value){
        String sql = "SELECT s FROM RunSitBean s WHERE s.appname = '"+value +"'";
        List<RunSitBean> list = dao.find(sql);
        int ON = 0;
        int OFF = 0;

        JSONObject jsonOb = new JSONObject();
        JSONArray ja = new JSONArray();
        if(list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
//                System.out.println(list.get(i).getUuid());
                if (list.get(i).getUuid() == null) {
                    OFF += 1;
                } else {
                    //根据uuid系统判断发送请求
                    String uri = "http://192.168.31.252/api/v1/mo/NetworkDevice/00100090/metrics/PingStatus";
//                String uri ="http://"+wsIp+"/api/v1/mo/NetworkDevice/"+list.get(i).getUuid()+"/metrics/PingStatus";
                    String data = InfUtil.interfaceUtil(uri, "");
                    JSONObject jsonObject = new JSONObject(data);
                    // 判断是否是好使状态
                    String values = jsonObject.get("value").toString();
                    if (values.equals("ON")) {
                        ON += 1;
                    } else {
                        ja.put(list.get(i).getUuid());
                    }

                }
            }
            if (ja.length()>0) {
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",3);
                jsonOb.put("msg",ja);
                return jsonOb;
            }else if(ON > 0){
                jsonOb.put("name",value);
                jsonOb.put("value",ON);
                jsonOb.put("type",1);
                return jsonOb;
            }else {
                jsonOb.put("name",value);
                jsonOb.put("value",OFF);
                jsonOb.put("type",2);
                return jsonOb;
            }
        }else {
            jsonOb.put("name",value);
            jsonOb.put("value",0);
            jsonOb.put("type",0);
            return jsonOb;
        }

    }
}
